// index.js
// 获取应用实例
const app = getApp()
// import { encryptedUserData } from '../../api/app.js'
import user from '../../utils/user.js'
import {
    encryptedPhoneData
} from '../../api/app.js'
Page({
    data: {
        top: app.globalData.menuTop,
        canIUseGetUserProfile: false,
        phone: '',
        isCheckbox: false
    },
    onLoad(options) {
        console.log('login已获取用户的', user)
        console.log(options, 'options')
        if (wx.getUserProfile) {
            this.setData({
                canIUseGetUserProfile: true
            })
        }
        if (!user.isNeedAuthSalaryCard) {
            user.reLogin().then(() => {
                app.toast('登陆成功', 'success', 2000)
                setTimeout(() => {
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                }, 2000)
            })
        }
    },
    onUnload() {
        app.globalData.homePath = ''
        console.log('页面卸载清空app.globalData.homePath', app.globalData.homePath)
    },
    getUserProfile() {
        wx.getUserProfile({
            lang: 'zh_CN',
            desc: '向团队成员展示头像信息',
            success(res) {
                console.log(res)
                user.avatarUrl = res.userInfo.avatarUrl
                wx.navigateTo({
                    url: '/pages/basic-form/basic-form'
                })
            },
            fail(err) {
                console.log(err)
            }
        })
    },
    getPhoneNumber(res) {
        if (!this.data.isCheckbox) {
            app.toast('请勾选用户协议及隐私协议')
            return
        }
        if (!user.session_key) {
            app.alert('操作过于频繁，请重新打开小程序').then(() => {
                wx.reLaunch({
                    url: '/pages/index/index',
                })
            })
            return
        }
        console.log(res, 'res')
        wx.navigateTo({
            url: '/pages/basic-form/basic-form',
            success: (res) => {
                res.eventChannel.emit('acceptDataFromOpenerPage', {
                    phone: ''
                })
            }
        })
    },
    getUserInfo(res) {
        user.avatarUrl = res.detail.userInfo.avatarUrl
        wx.navigateTo({
            url: '/pages/basic-form/basic-form'
        })
    },
    onGoUserAgreement() {
        wx.navigateTo({
            url: '/pages/user-agreement/user-agreement',
        })
    },
    onGoPrivacyPolicy() {
        wx.navigateTo({
            url: '/pages/privacy-policy/privacy-policy',
        })
    },
    // 单选事件
    onCheckbox(event) {
        console.log(event, 'event')
        this.setData({
            isCheckbox: !this.data.isCheckbox
        })
    },
    back() {
        wx.switchTab({
            url: '/pages/index/index'
        })
    }
})